package app 
{
	import app.stm.BaseSystem;
	import app.stm.sync.SynchronizationSystem;
	import flash.display.Sprite;
	import flash.events.Event;
	import net.NetDistribute;
	import net.SocketConnect;
	import ticker.manager.FrameTickerManager;
	import ticker.manager.TimeTickManager;
	import ticker.TickerConstant;
	import ticker.TickerPlug;
	
	/**
	 * 主程序的启动类
	 * @author gcxk721
	 */
	public class Application extends Sprite 
	{
		
		public static var sysLst : Vector.<BaseSystem> = new Vector.<BaseSystem>(127);
		
		public static var netConnect : SocketConnect;
		
		public function Application() 
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
		}
		
		private function onAddedToStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
			tickerInit();
			systemInit();
			networkInit();
		}

		/**
		 * 初始化整个程序的Ticker管理器
		 */
		private function tickerInit():void 
		{
			TickerPlug.instance.addManager(new TimeTickManager(TickerConstant.ONE_SECOND));
			TickerPlug.instance.addManager(new FrameTickerManager(TickerConstant.ONE_FRAME));
			TickerPlug.instance.start(this.stage);
		}
		
		/**
		 * 初始化所有的系统
		 */
		private function systemInit():void 
		{
			sysLst[SynchronizationSystem.instance.serverId] = SynchronizationSystem.instance;
		}
		
		/**
		 * 进行网络通信的初始化
		 */
		private function networkInit():void 
		{
			netConnect = new SocketConnect(NetDistribute.instance);
			netConnect.addEventListener(Event.CONNECT, onSocketConnectedHandler);
			netConnect.connect("127.0.0.1", 11024);
		}
		
		private function onSocketConnectedHandler(e:Event):void 
		{
			trace("connected");
		}
		
	}

}